/**
 * sockettj  -- A php language extension application for network socket.
 * Copyright (C) 2011  degui.chen <degui.chen@qq.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <netdb.h>
#include <net/if.h>
#include "sockettj.h"

static int socket_connect;
unsigned char local_ip[20];
unsigned int port_data = 48589;
int recv_line() {
	char buffer[1024];
	int length = recv(socket_connect, buffer, 1024, 0);
	buffer[length] = '\0';
	printf("%s", buffer);
	return atoi(buffer);
}

void recv_lines( fd) {
	char buffer[1024];
	int length;
	while (1) {
		length = recv(fd, buffer, 1024, 0);
		if (1 > length) {
			break;
		}
		buffer[length] = '\0';
		printf("--%s", buffer);
	}
}

int command_send(char* username) {
	char str[200] = { '\0' };
	sprintf(str, "ftp>%s", username);
	printf("%s", str);
	send(socket_connect, username, strlen(username), 0);
	return recv_line();
}

void create_port_comment(char* port_cmd) {
	port_data++;
	int part1 = 0;
	while (port_data > (part1 * 256)) {
		part1++;
	}
	part1--;
	int padding = port_data - (part1 * 256);
	sprintf(port_cmd, "PORT %s,%i,%i\r\n", local_ip, part1, padding);
}

int socket_get_file(int port, char* save_path) {
	int bind_socket = open_bind(port);
	int address_length = sizeof(struct sockaddr_in);
	struct sockaddr_in address_client;
	int accept_fd;
	if ((accept_fd = accept(bind_socket, (struct sockaddr *) &address_client, &address_length)) == -1) {
		perror("accept() error\n");
		exit(1);
	}
	printf("You got a connection from %s\n", inet_ntoa(address_client.sin_addr));
	char buffer[1024];
	int length;
	int fd = open(save_path, O_CREAT | O_RDWR);
	if (-1 == fd) {
		perror("open");
		return (-1);
	}
	while (1) {
		length = recv(accept_fd, buffer, 1024, 0);
		if (1 > length) {
			break;
		}
		write(fd, buffer, length);
		buffer[length] = '\0';
		printf("--%s => '%s' \n", save_path, buffer);
	}
	close(fd);
	close(accept_fd);
	close(bind_socket);
	return 0;
}

void command_get_file(char* get, char* save_path) {
	char ports[256] = { '\0' };
	create_port_comment(&ports);
	send(socket_connect, ports, strlen(ports), 0);
	send(socket_connect, get, strlen(get), 0);
	socket_get_file(port_data, save_path);
	recv_line();
	recv_line();
}

void socket_put_file(int port, char *path[]) {
	int bind_socket = open_bind(port);
	int address_length = sizeof(struct sockaddr_in);
	struct sockaddr_in address_client;
	int df_connect;
	if ((df_connect = accept(bind_socket, (struct sockaddr*) &address_client, &address_length)) == -1) {
		perror("accept() error\n");
		exit(1);
	}
	printf("You got a connection from %s\n", inet_ntoa(address_client.sin_addr));
	send_file(path, df_connect);
	close(df_connect);
	close(bind_socket);
}

void command_put_file(char put[], char path[]) {
	char ports[256] = { '\0' };
	create_port_comment(&ports);

	send(socket_connect, ports, strlen(ports), 0);

	send(socket_connect, put, strlen(put), 0);
	socket_put_file(port_data, path);
	recv_line();
	recv_line();
}
int login(char* username, char* password) {
	char cmd[255] = { '\0' };
	sprintf(cmd, "USER %s\r\n", username);
	if (331 != command_send(cmd)) {
		return -1;
	}

	sprintf(cmd, "PASS %s\r\n", password);
	if (230 != command_send(cmd)) {
		return -1;
	}

	return 0;
}
int ftp_get(char* ip, int port, char* username, char* password, char* dirname, char* filename, char* save_path) {
	strcpy(local_ip, ip_search());
	change_ip_address(local_ip);

	socket_connect = create_client_tcp(ip, port);
	recv_line();

	if (login(username, password)) {
		return -1;
	}
	char type[] = "TYPE I\r\n";
	command_send(type);

	char cwd[255] = { '\0' };
	sprintf(cwd, "CWD %s\r\n", dirname);
	command_send(cwd);

	char get[255] = { '\0' };
	sprintf(get, "RETR %s\r\n", filename);

	command_get_file(get, save_path);
	close(socket_connect);
}

int ftp_put(char* ip, int port, char* username, char* password, char* dirname, char* filepath) {
	strcpy(local_ip, ip_search());
	change_ip_address(local_ip);

	socket_connect = create_client_tcp(ip, port);
	recv_line();

	if (login(username, password)) {
		return -1;
	}
	char type[] = "TYPE I\r\n";
	command_send(type);

	char cwd[255] = { '\0' };
	sprintf(cwd, "CWD %s\r\n", dirname);
	command_send(cwd);

	char name[255] = { '\0' };
	get_file_name(filepath, name);

	char put[255] = { '\0' };
	sprintf(put, "STOR %s\r\n", name);
	char path[] = "/etc/hosts";
	command_put_file(put, filepath);
	close(socket_connect);
}
